$(function () {
    var client_datagrid = $("#client_datagrid");
    var client_dialog = $("#client_dialog");
    var client_form = $("#client_form");

// 数据 表
    client_datagrid.datagrid({
        url: "/client/query.do?&clientState=2&clientState=3&clientState=4",
        fitColumns: true, // 自适应列
        fit: true,
        striped: true,  // 斑马线
        pagination: true,
        rownumbers: true,
        pageSize: 10,
        pageNumber: 1,
        singleSelect: true, // 单选
        toolbar: "#client_toolbar",
        columns: [
            [
                {field: 'id', hidden: true},
                {
                    field: 'state', title: '客户状态', width: 80,
                    formatter: function (value) {
                        switch (value) {
                            case 2:
                                return '资源池客户';
                            case 3:
                                return '开发失败客户';
                            case 4:
                                return '流失客户';
                        }
                    }
                },
                {field: 'inputdate', title: '创建日期', width: 80, sortable: true},
                {
                    field: 'inputuser', title: '创建人', width: 80,
                    formatter: function (value) {
                        return value ? value.username : "";
                    }
                },
                {
                    field: 'chargeuser', title: '负责人', width: 80,
                    formatter: function (value) {
                        return value ? value.username : "";
                    }
                },
                {field: 'name', title: '客户', width: 80,},
                {field: 'age', title: '年龄', width: 80},
                {
                    field: 'gender', title: '性别', width: 80,
                    formatter: function (value, row, index) {
                        return value ? "男" : "女";
                    }
                },
                {field: 'job', title: '职业', width: 80},
                {field: 'salary', title: '薪资水平', width: 80},
                {field: 'tel', title: '电话', width: 80},
                {field: 'email', title: '邮箱', width: 80},
                {field: 'address', title: '客户地址', width: 80},
                {field: 'source', title: '客户来源', width: 80},
                {field: 'qq', title: 'QQ', width: 80},
                {field: 'wechat', title: '微信', width: 80},
                {field: 'identitynum', title: '身份证号', width: 80},
            ]
        ]
    });
    // 对话 框
    client_dialog.dialog({
        width: 320,
        height: 420,
        top: 100,
        buttons: "#form_btns",
        closed: true,
        onClose: function () {
            client_form.form("clear");
        }
    });

    function openSetTitle(str) {
        client_dialog.dialog("setTitle", str)
        client_dialog.dialog("open")
    }

    var objMethod = {
        add: function () {
            $(".myPassword").show();
            openSetTitle("新增客户")
        },

        edit: function () {
            $(".myPassword").hide();
            var row = client_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", '请选择一条数据', 'warning')
                return
            }
            if (row.dept) {
                row["dept.id"] = row.dept.id
            }
            // 回显数据
            client_form.form("load", row);

            openSetTitle("编辑客户");

        },
        changeState: function () {
            var row = client_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", '请选择一条数据', 'warning')
                return
            }
            $.messager.confirm('确认', '你确认要操作吗？', function (r) {
                if (!r) return
                $.get('/client/changeState.do', {id: row.id}, function (data) {
                    if (data.success) {
                        $.messager.alert("温馨提示", '操作成功', 'info', function () {
                            client_datagrid.datagrid("reload");
                        })
                    } else {
                        $.messager.alert("温馨提示", '操作失败', 'info', function () {
                        })
                    }
                })
            })
        },
        save: function () {
            // 提交表单
            client_form.form("submit", {
                url: "/client/saveOrUpdate.do",
                onSubmit: function (params) {
                    return client_form.form('validate');
                },
                success: function (data) {
                    data = $.parseJSON(data)
                    if (data.success) {
                        $.messager.alert("温馨提示", '保存成功', 'info', function () {
                            objMethod.cancel();
                            client_datagrid.datagrid("reload");
                        })
                    } else {
                        $.messager.alert("温馨提示", '保存失败', 'info', function () {
                        })
                    }
                }
            })
        },
        cancel: function () {
            client_dialog.dialog("close")
        },
        query: function () {
            var minInputDate = $("#minInputDate").textbox('getValue');
            var maxInputDate = $("#maxInputDate").textbox('getValue');
            var identitynum = $("#identitynum").textbox('getValue');
            var tel = $("#tel").textbox('getValue');
            var principal = $("#principal").textbox('getValue');
            var keyword = $("#keyword").textbox('getValue');
            $('#client_datagrid').datagrid('load', {
                minInputDate: minInputDate,
                maxInputDate: maxInputDate,
                identitynum: identitynum,
                tel: tel,
                principal: principal,//负责人
                keyword: keyword
            })
        },
        absorb:function () {
            var row = client_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", '请选择一条数据', 'warning')
                return
            }
            $.messager.confirm('确认', '你确认要吸纳该客户吗？', function (r) {
                if (!r) return;
                $.get('/client/absorb.do', {id: row.id}, function (data) {
                    if (data.success) {
                        $.messager.alert("温馨提示", '操作成功', 'info', function () {
                            client_datagrid.datagrid("reload");
                        })
                    } else {
                        $.messager.alert("温馨提示", '操作失败', 'info', function () {
                        })
                    }
                })
            })
        },
        refresh: function () {
            //刷新表单
            client_datagrid.datagrid("reload");
        }

    };
    // 绑定事件
    $('a[data-cmd]').click(function () {
        objMethod[$(this).data('cmd')]();
    })
})

